<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px" label-position="right" class="common-border pt10 mb10">
      <div>
        <el-form-item label="方案名称" prop="planName">
          <el-input
            v-model="queryParams.planName"
            placeholder="请输入"
            clearable
            style="width:180px"
            :maxlength="50"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="计费方式" prop="feeMode">
          <el-select clearable filterable style="width:180px" placeholder="请选择" v-model="queryParams.feeMode">
            <el-option
              v-for="(item, index) in dict.type.price_method"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item label="状态" prop="status">
          <el-select clearable filterable style="width:180px" placeholder="请选择" v-model="queryParams.status">
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            style="width:180px"
            v-model="queryParams.createTime"
            type="date"
            value-format="yyyy-MM-dd"
            clearable
            placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
<!--          <el-button type="primary" size="mini" @click="handleAdd(undefined, 'add')">新增</el-button>-->
          <el-button type="primary" size="mini" @click="handleQuery">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd(undefined, 'add')"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      v-table-height="{bottomOffset: 75, callback: height => tableHeight = height}"
      :height="tableHeight"
    >
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="方案名称" align="center" prop="planName" min-width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-tag v-if="(!scope.row.lineList || !scope.row.lineList.length) && !scope.row.districtId" size="mini">通用价格</el-tag>{{scope.row.planName}}
        </template>
      </el-table-column>
      <el-table-column label="计费方式" align="center" prop="feeMode" min-width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{selectDictLabel(dict.type.price_method, scope.row.feeMode)}}
        </template>
      </el-table-column>
      <el-table-column label="计费标准" align="center" prop="segList" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <!--          <template v-if="scope.row.lineList && scope.row.lineList.length">-->
          <template v-if="scope.row.feeMode === 'weightMultiMileage'">
            <template v-if="scope.row.feeMode === 'weightMultiMileage'">
              <p>最低计重 {{scope.row.waterProof}}吨</p>
              <div v-for="(item,index) in scope.row.segList.filter(v => v.feeMode === 'mileage')" :key="item.id">
                <span v-if="index === 0">{{getTypeName(item.feeMode)}}:</span>
                <span v-if="index === 0">{{getTypeName3(item.feeMode)}}</span>
                <span v-else>{{getTypeName4(item.feeMode)}}</span>
                <span v-if="index !== scope.row.segList.filter(v => v.feeMode === 'mileage').length - 1">{{item.floatStart}}KM~{{item.floatEnd}}KM</span>
                <span v-else>{{item.floatStart}} KM </span>
                <span class="ml10">{{item.price}} 元/吨,</span>
              </div>
            </template>
            <template v-if="scope.row.feeMode === 'mileage' || scope.row.addFeeMode === 'mileage'">
              <div v-for="(item,index) in scope.row.segList.filter(v => v.feeMode === 'mileage')" :key="item.id">
                <span v-if="index === 0">{{getTypeName(item.feeMode)}}:</span>
                <span v-if="index === 0">{{getTypeName3(item.feeMode)}}</span>
                <span v-else>{{getTypeName4(item.feeMode)}}</span>
                <span v-if="index !== scope.row.segList.filter(v => v.feeMode === 'mileage').length - 1">{{item.floatStart}}KM~{{item.floatEnd}}KM</span>
                <span v-else>{{item.floatStart}} KM </span>
                <span class="ml10">{{item.price}} 元/吨,</span>
              </div>
            </template>
            <template v-if="scope.row.feeMode !== 'mileage'">
              <div v-for="(item,index) in scope.row.segList.filter(v => v.feeMode === scope.row.feeMode)" :key="item.id">
                <span v-if="index === 0">{{getTypeName(item.feeMode)}}:</span>
                <span v-if="item.feeMode === 'volumeWeight' && index === 0">轻泡系数:{{scope.row.waterProof}},</span>
                <span v-if="index === 0">{{getTypeName3(item.feeMode)}}</span>
                <span v-else>{{getTypeName4(item.feeMode)}}</span>
                <template v-if="scope.row.feeMode !== 'pieces'">
                  <span v-if="index !== scope.row.segList.filter(v => v.feeMode === scope.row.feeMode).length - 1">{{item.floatStart}}{{getTypeName5(item.feeMode)}}~{{item.floatEnd}}{{getTypeName5(item.feeMode)}}</span>
                  <span v-else>{{item.floatStart}}{{getTypeName5(item.feeMode)}}</span>
                </template>
                <template v-else>
                  <span v-if="index !== scope.row.segList.filter(v => v.feeMode === scope.row.feeMode).length - 1">{{item.initStart}}{{getTypeName5(item.feeMode)}}~{{item.initEnd}}{{getTypeName5(item.feeMode)}}</span>
                  <span v-else>{{item.initStart}}{{getTypeName5(item.feeMode)}}</span>
                </template>
                <span class="ml10">{{item.price}}元,</span>
              </div>
            </template>
          </template>
          <template v-else>未设置</template>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.status === '0'">启用</span>
          <span v-else-if="scope.row.status === '1'">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="最近修改时间" align="center" prop="updateTime" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="创建人" align="center" prop="createBy"  width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row, 'edit')"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />
    <Detail ref="detail" />
    <Add ref="add"  @close="getList" />
    <Add ref="edit" :isOpen="isOpen" @close="getList" />
  </div>
</template>

<script>
import {
  pricePlanList,
  deletePricePlan,
} from "@/api/system/price";
import Add from './add.vue'
import Detail from './detail.vue'
import { getAllCityInfo  } from "@/api/login";
var cloneDeep = require('lodash.clonedeep');

export default {
  components: {
    Add,
    Detail,
  },
  dicts: ['transport_type','price_method'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
      },
      tableHeight: 0,
      cardHeight: 0,
      autoKey: 0,

      isOpen: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      const queryParams = cloneDeep(this.queryParams)
      queryParams.pageSize = this.pageSize
      queryParams.pageNum = this.pageNum
      queryParams.districtId = queryParams.districtId && queryParams.districtId.join(',')
      pricePlanList(queryParams).then(response => {
        this.list = response.rows.filter(item => item.feeMode === 'weightMultiMileage');
        this.isOpen = response.rows.find(item => item.status === '0')?.id || ''
        this.list = this.list.map((v,k) => {
          return {
            ...v,
            mileageList: v.feeMode === 'mileage' ? v.segList : v.addFeeMode === '1' ? v.segList.filter(v1 => v1.feeMode === 'mileage') : [],
            weightList: v.feeMode === 'weight' ? v.segList.filter(v1 => v1.feeMode === 'weight') : [],
            volumeList: v.feeMode === 'volume' ? v.segList.filter(v1 => v1.feeMode === 'volume') : [],
            piecesList: v.feeMode === 'pieces' ? v.segList.filter(v1 => v1.feeMode === 'pieces') : [],
            volumeWeightList: v.feeMode === 'volumeWeight' ? v.segList.filter(v1 => v1.feeMode === 'volumeWeight') : [],
          }
        })
        this.total = response.total;
      }).finally(()=> {
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    handleDetail(row) {
      this.$refs.detail.showDialog(row)
    },
    handleEdit(row) {
      this.$refs.edit.showDialog(row, 'edit')
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除方案名称为"' + row.planName + '"的数据项？').then(function() {
        return deletePricePlan(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 新增
    handleAdd() {
      this.$refs.edit.showDialog(undefined, 'add')
    },

    getTypeName(type) {
      return {
        mileage: '按里程',
        weight: '按重量',
        volume: '按体积',
        pieces: '按件数',
        volumeWeight: '按体积重量',
        weightMultiMileage: '按重量里程',
      }[type] || ''
    },
    getTypeName2(type) {
      return {
        mileage: '实际里程',
        weight: '实际重量',
        volume: '实际体积',
        pieces: '实际件数',
        volumeWeight: '体积重量',
        weightMultiMileage: '重量里程',
      }[type] || ''
    },
    getTypeName3(type) {
      return {
        mileage: '首里程',
        weight: '首重',
        volume: '首容量',
        pieces: '首件',
        volumeWeight: '首重',
        weightMultiMileage: '首里程',
      }[type] || ''
    },
    getTypeName4(type) {
      return {
        mileage: '续里程',
        weight: '续重',
        volume: '续容量',
        pieces: '续件',
        volumeWeight: '续重',
        weightMultiMileage: '续里程',
      }[type] || ''
    },
    getTypeName5(type) {
      return {
        mileage: 'KM',
        weight: 'KG',
        volume: '立方',
        pieces: '件',
        volumeWeight: 'KG',
        weightMultiMileage: 'KM',
      }[type] || ''
    },
  }
};
</script>
<style lang="scss">

</style>

